package com.woniuxy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.PageDto;
import com.woniuxy.entity.CategoryOne;
import com.woniuxy.entity.CategoryTwo;
import com.woniuxy.entity.HeatBidding;
import com.woniuxy.entity.Product;
import com.woniuxy.mapper.BiddingMapper;
import com.woniuxy.mapper.ProductMapper;
import com.woniuxy.service.LogService;
import com.woniuxy.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	ProductMapper productMapper;

	@Autowired
	LogService LogService;

	@Autowired
	private BiddingMapper biddingMapper;

	@Override
	public List<HeatBidding> findHeatBidding() {// 热拍卖品
		List<HeatBidding> findHeatBidding = productMapper.findHeatBidding();
		return findHeatBidding;
	}

	@Override
	public List<CategoryTwo> findCategory(int co_id) {
		return productMapper.findCategory_two(co_id);
	}

	@Override
	public PageDto<Product> findByCategory(int co_id, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = productMapper.findByCategory(co_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByCt_id(int co_id, int ct_id, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		Product product = new Product().setCo_id(co_id).setCt_id(ct_id);
		List<Product> list = productMapper.findByCt_id(product);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;

	}

	@Override
	public PageDto<Product> findByName(String name, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		int ct_id = productMapper.findCt_idByName(name);
		List<Product> list = productMapper.findByName(ct_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByStatus(int audit_status, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = productMapper.findByStatus(audit_status);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByStatusAndName(String name, int audit_status, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		int ct_id = productMapper.findCt_idByName(name);
		Product product = new Product().setCt_id(ct_id).setAudit_status(audit_status);
		List<Product> list = productMapper.findByStatusAndName(product);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public List<Product> findHeat() {
		List<Product> list = productMapper.findHeat();
		updateData(list);
		return list;
	}

	@Override
	public List<Product> findHeat2() {
		List<Product> list = productMapper.findHeat2();
		updateData(list);
		return list;
	}

	@Override
	public PageDto<Product> findByLike(String name, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		name = "%" + name + "%";
		PageDto<Product> pageDto = new PageDto<>();
		List<Product> list = productMapper.findByLike(name);
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	// 分页封装
	public PageDto<Product> pageInfo(int pageNum, List<Product> list, PageDto<Product> pageDto) {
		updateData(list);
		PageHelper.startPage(pageNum, 6);
		PageInfo<Product> pageInfo = new PageInfo<>(list);
		pageDto.setCurrentPage(pageNum);
		pageDto.setPageSize(6);
		pageDto.setTotal(pageInfo.getTotal());// 总条数
		pageDto.setTotalPages(pageInfo.getPages());// 总页数
		pageDto.setData(list);
		return pageDto;
	}

	// 修改成用户看见的信息
	public List<Product> updateData(List<Product> list) {
		for (Product product : list) {
			product.setPicture("images/" + product.getPicture());
			if (product.getName().length() > 10) {
				product.setName(product.getName().substring(0, 10) + "...");
			}
			if (product.getAudit_status() == 1) {
				product.setChargeNum(0);
				product.setDetails("即将拍卖");
			} else if (product.getAudit_status() == 2) {
				product.setChargeNum(biddingMapper.findNum(product.getP_id()));
				product.setDetails("正在拍卖");
			} else if (product.getAudit_status() == 3) {
				product.setChargeNum(biddingMapper.findNum(product.getP_id()));
				product.setDetails("已结束");
			}
		}
		return list;
	}

	@Override
	public PageDto<Product> findByNewest(int co_id, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = productMapper.findByNewest(co_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByHotest(int co_id, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = productMapper.findByHotest(co_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByChargeNum(int co_id, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = productMapper.findByChargeNum(co_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public PageDto<Product> findByPriceUpAndDown(int co_id, boolean flag, int pageNum) {
		PageHelper.startPage(pageNum, 6);
		List<Product> list = new ArrayList<>();
		if (flag)
			list = productMapper.findByPriceDown(co_id);
		else
			list = productMapper.findByPriceUp(co_id);
		PageDto<Product> pageDto = new PageDto<>();
		pageInfo(pageNum, list, pageDto);
		return pageDto;
	}

	@Override
	public int updateHeat(int p_id) {
		return productMapper.updateHeat(p_id);
	}

	@Transactional
	@Override
	public Product findProductById(int p_id, int u_id) {// 查询good详细信息
		// TODO Auto-generated method stub

		Product Product = productMapper.findProductById(p_id, u_id);

		return Product;
	}

	@Override
	public Boolean isCollected(int p_id, int u_id) {// 是否收藏过该商品
		// TODO Auto-generated method stub
		int isCollected = productMapper.isCollected(p_id, u_id);
		return isCollected == 1 ? true : false;
	}

	@Override
	public boolean isSigned(int p_id, int u_id) {// 是否预约过
		// TODO Auto-generated method stub
		int signed = productMapper.isSigned(p_id, u_id);
		return signed == 1 ? true : false;
	}

	@Override
	public PageDto<Product> goodesByUid(int uid, int page) {

		List<Product> goodes = new ArrayList<Product>();

		PageHelper.startPage(page, 5);
		goodes = productMapper.goodesByUid(uid);
		PageInfo<Product> pageInfo = new PageInfo<>(goodes);

		PageDto pageDto = new PageDto();
		pageDto.setCurrentPage(page); // 当前页
		pageDto.setPageSize(5); // 每一页大小
		pageDto.setTotal(pageInfo.getTotal()); // 总条数
		pageDto.setTotalPages(pageInfo.getPages()); // 总页数
		pageDto.setData(goodes); // 商品数据

		return pageDto;

	}

	@Override
	public List<CategoryOne> findoneMenu() {
		return productMapper.findoneMenu();
	}

	@Override
	public List<CategoryTwo> findTwoMenu() {
		return productMapper.findTwoMenu();
	}

	// 添加商品方法
	public boolean addProduct(Product product) {

		return productMapper.addProduct(product);
	}

	@Override
	public PageInfo<Product> allProducts(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Product> list = productMapper.allProducts();
		PageInfo<Product> page = new PageInfo<Product>(list);

		return page;

	}

	@Override
	public int batchUpdateProducts(Integer audit_status, int[] ids) {

		return productMapper.batchUpdateProducts(audit_status, ids);
	}

	@Override
	public int batchDeleteProducts(int[] ids) {

		return productMapper.batchDeleteProducts(ids);
	}

}
